/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.rms.events.recordstores.marks;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.rms.RecordListener;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

import mtt.rms.dataObjects.Mark;
import mtt.rms.events.recordstores.marks.events.AddMarkRecordStoreListenerEvent;
import mtt.rms.events.recordstores.marks.events.GetNumMarkRecordsEvent;
import mtt.rms.events.recordstores.marks.events.GetVectorMarksEvent;
import mtt.rms.events.recordstores.marks.events.RemoveMarkEvent;
import mtt.rms.events.recordstores.marks.events.SetNumMarkRecordsEvent;
import mtt.rms.events.recordstores.marks.events.SetVectorMarksEvent;
import mtt.rms.events.recordstores.marks.events.StoreMarkEvent;
import mtt.rms.events.recordstores.marks.events.UpdateMarkEvent;

import hmvc.j2me.component.IComponent;
import hmvc.j2me.events.IEvent;
import hmvc.j2me.framework.rms.ObjectStore;

/**
 * 
 * 
 * @author jens.meiss
 */
public class RecordStoreMarks extends ObjectStore {

	/**
	 * specify the name of the "Marks"-RecordStore name
	 */
	private static String nameStorageMarks = "MTT_Noten";

	protected String getRecordStoreName() {
		return nameStorageMarks;
	}

	public void handleEvent(IEvent event) {
		try {
			if (event instanceof GetVectorMarksEvent) {

				handleEventGetVectorMarks((GetVectorMarksEvent) event);

			} else if (event instanceof StoreMarkEvent) {

				storeMark((Mark) event.getPayload());

			} else if (event instanceof RemoveMarkEvent) {

				removeMark((Mark) event.getPayload());

			} else if (event instanceof UpdateMarkEvent) {

				updateMark((Mark) event.getPayload());

			} else if (event instanceof AddMarkRecordStoreListenerEvent) {

				addRecordListener((RecordListener) event.getPayload());

			} else if (event instanceof GetNumMarkRecordsEvent) {

				GetNumMarkRecordsEvent getNumMarksRecordsEvent = (GetNumMarkRecordsEvent) event;
				IComponent component = getNumMarksRecordsEvent.getComponent();
				component.fireEventLocal(new SetNumMarkRecordsEvent(getNumRecords()));

			} 
			
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void handleEventGetVectorMarks(
			GetVectorMarksEvent eventGetVectorMarks)
			throws RecordStoreException, IOException {

		/*
		 * exsistieren Records
		 */
		if (getNumRecords() == 0) {
			eventGetVectorMarks.getComponent().fireEvent(
					new SetVectorMarksEvent(null), false, true, false);
			return;
		}

		Vector vectorMarks = getVectorRecords(new Mark(), null, null, false);

		eventGetVectorMarks.getComponent().fireEvent(
				new SetVectorMarksEvent(vectorMarks), false, true, false);
	}

	public void init() {
		open();
	}

	public void registerEvents() {
		register(GetVectorMarksEvent.class, this);
		register(StoreMarkEvent.class, this);
		register(RemoveMarkEvent.class, this);
		register(UpdateMarkEvent.class, this);
		register(AddMarkRecordStoreListenerEvent.class, this);
		register(GetNumMarkRecordsEvent.class, this);
	}

	private void removeMark(Mark mark) throws RecordStoreException {
		remove(mark.getRecordId());
	}

	private void storeMark(Mark mark) throws RecordStoreException, IOException {
		store(mark);
	}

	public void terminate() {
		close();
	}

	private void updateMark(Mark mark) throws RecordStoreException, IOException {
		update(mark);
	}

}
